app.register.controller('detailCtrl',['$scope','$location','$stateParams','$http','Context','$cookies','$timeout','$q',
    function ($scope,$location,$stateParams,$http,Context,$cookies,$timeout,$q) {
    // console.log($cookies.strTokenId);

    // var nEventId = base64.decode($stateParams.nEventId);
    //     console.log($stateParams.nEventId);
    $scope.swiper = {};
     $scope.onReadySwiper = function (swiper) {
         swiper.on('slideChangeStart', function () {
            console.log('slideChangeStart');
         });
     };


    //活动保存id
    $cookies.nEventId = $stateParams.nEventId;
    // console.log($cookies. );

    // 请求页面数据
    $http({
        method:'POST',
        url:$config.host+'/api/Events/GetEventDetails',
        params:{
            nEventId : $stateParams.nEventId,
            nCurPage :1,
            nPageSize :10
        }
    }).success(function (json) {
        // console.log(JSON.stringify(json));
        if(json.nFlag==1){
            console.log('请求数据成功');
            // 传递页面数据
            $scope.eventDetails = json.data.eventDetails;
            $scope.eventComment = json.data.eventComment;
            $scope.eventStatus =$scope.eventDetails.eventStatus;
            // $scope.eventStatus = 0;
            if($scope.eventStatus == 0){
                $scope.signupstate = '报名';
                $scope.bgcolor = 1;
            }else if($scope.eventStatus == 1){
                $scope.signupstate = '已报名';
                console.log('ds')
            }else if($scope.eventStatus == 2 || $scope.eventStatus == 3){
                $scope.signupstate = '已过期';
            }
            //延迟执行加载更多判断
            $timeout(function () {
                $scope.isshowmore();
            },0)
        }else{
            $scope.detailpopshow(json.strMessage);
        }
    })

    // 点击我要报名
    $scope.issignup =function () {
        //判断报名的状态
        // console.log($scope.eventStatus);
        if($scope.eventStatus == 0 && $scope.signupstate == '报名'){

        }else{
            return;
        }

        //判断是否登录
        if(!Context.isUserLogin()){
            //去登录
            $location.path('/login');
        }else{
            console.log($cookies.strTokenId);
            //判断活动是否收费
            if(!$scope.eventDetails.isShouFei){
                console.log('不收费');
                console.log($cookies.strTokenId);
                console.log($cookies.nUserId);
                /*$scope.detailpopshow("测试呀");*/
                //不收费直接去报名
                $http({
                    method:'POST',
                    url:$config.host+'/api/events/EventSignUp',
                    headers:{
                        UID :$cookies.nUserId,
                        TOKENID : $cookies.strTokenId
                    },
                    params:{
                        nEventId : $stateParams.nEventId
                    }
                }).success(function (json) {
                    // console.log(JSON.stringify(json));
                    if(json.nFlag==1) {
                        $scope.detailpopshow(json.strMessage);
                        $scope.signupstate = '已报名';
                        $scope.bgcolor = 0;
                    }else{
                        console.log('报名失败')
                        $scope.detailpopshow(json.strMessage);
                    }
                }).error(function(json){
                    console.log('接口错误失败')
                    $scope.detailpopshow(json.strMessage);
                });
            }else{
                //收费
                $scope.detailpopshow("收费活动，敬请期待！");
            }

        }
    }

    // 过滤图片用户头像
    $scope.itemStrHeadPhotoThum = function (value) {
        return value?$scope.config.host+value:__uri('/src/image/login/touxiang.png');
    }

    //详情加空格
    $scope.detailsFunc = function (value) {
        var str = value.replace(/\r\n/g,'<br>');
            str = str.replace(/[\r,\n]/g,'<br>');
        return str;
    }

   //根据详情内容是否显示查看更多
    var cont_H, box_H;
    $scope.isshowmore = function () {
        box_H = $('.article').height();
        cont_H = $('.article-cont').height();
        // console.log(cont_H,box_H);
        if(cont_H>box_H){
            $scope.test = '查看更多';
            $('.article').css({'maxHeight':'none'}).height(box_H);
        }else{
            $scope.test = '';
        }
    }

    //活动简介内容点击查看更多
    $scope.clickSeemore =function () {
        if($scope.test == '查看更多'){
            $scope.test = '点击收起';
            $('.article').height(cont_H);
        }else if($scope.test == '点击收起'){
            $scope.test = '查看更多';
            $('.article').height(box_H);
        }

    }

    //报名返回结果弹框
    $scope.detailpopshow = function (value,time,func) {
        if(time){
            var speed = time;
        }else{
            var speed = 2000;
        }
        $scope.isdetailpopshow = value;
        $timeout(function () {
            $scope.isdetailpopshow = false;
            if(func) func();
        },speed)
    }

    //点击图片
    /*$scope.clickImgboost =function (index) {
        var winheight = $config.windowHeight();
        $scope.bgimgLineHeight = winheight;
        $scope.bigCutView = index;
        $scope.isboostimg = true;
    }*/

    $scope.clickImgboost =function (index) {
        var winheight = $config.windowHeight();
        $scope.bgimgLineHeight = winheight;
        $scope.bigCutView = index;
        $scope.isboostimg = true;
        $scope.boxisfade ='fadeIn';
        $scope.isfade = 'zoomIn';
    }


    //点击关闭弹框
        $scope.closeboostimg = function () {
            $scope.boxisfade ='fadeOut';
            $scope.isfade = 'zoomOut';
            $timeout(function () {
                $scope.isboostimg = false;
            },600)
        }


}]);
